import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Solution1306 {
    public boolean canReach(int[] arr, int start) {
        int[] visit=new int[arr.length];
        Arrays.fill(visit,Integer.MAX_VALUE);
        Queue<Integer> queue=new LinkedList<>();
        queue.offer(start);
        visit[start]=0;
        while (!queue.isEmpty()){
            int q=queue.poll();
            if (arr[q]==0){
                return true;
            }
            if (q-arr[q]>=0 && visit[q-arr[q]]==Integer.MAX_VALUE){
                visit[q-arr[q]]=0;
                queue.offer(q-arr[q]);
            }
            if (q+arr[q]<arr.length && visit[q+arr[q]]==Integer.MAX_VALUE){
                visit[q+arr[q]]=0;
                queue.offer(q+arr[q]);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1306().canReach(new int[]{4,2,3,0,3,1,2},5));
    }
}
